package com.hospital.diagnosis.module.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import com.hospital.diagnosis.module.hanlder.DashScopeErrorHandler;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RagConfig {
    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;
    @Value("${rag.index-name}")
    private String indexName;
    @Autowired
    private DashScopeErrorHandler dashScopeErrorHandler;

    /**
     * 创建知识检索器
     */
    @Bean
    public DocumentRetriever healthcareDocumentRetriever(DashScopeApi dashScopeApi) {
        return new DashScopeDocumentRetriever(dashScopeApi, DashScopeDocumentRetrieverOptions.builder()
                .withIndexName(indexName)
                .build());
    }

    /**
     * 创建dashScopeApi基本配置
     */
    @Bean
    public DashScopeApi dashScopeApi() {
        return DashScopeApi.builder()
                .apiKey(apiKey)
                .responseErrorHandler(dashScopeErrorHandler)
                .build();
    }
}
